<template>
  <el-aside :width="isCollapse ? '64px' : '200px'">
    <!-- 折叠栏 -->
    <div v-show="!isCollapse" class="toggle-button" @click="toggleCollapse">
      <i class="iconfont icon-sanshu"></i>
    </div>
    <div v-show="isCollapse" class="toggle-button" @click="toggleCollapse">
      <i class="iconfont icon-zhedie"></i>
    </div>
    <el-col>
      <el-menu
        background-color="#333744"
        text-color="#fff"
        active-text-color="#409EFF"
        unique-opened
        :collapse="isCollapse"
        :collapse-transition="false"
        router
        :default-active="activePath"
      >
        <el-submenu
          :index="item.id + ''"
          v-for="item in asideListData.menuList"
          :key="item.id"
        >
          <!-- 一级菜单 -->
          <template slot="title">
            <i :class="iconList[item.id]" class="el-icon-menu iconfont"></i>
            <span>{{ item.authName }}</span>
          </template>
          <!-- 二级菜单 -->
          <el-menu-item
            :index="'/' + childrenList.path"
            v-for="childrenList in item.children"
            :key="childrenList.id"
            @click="saveNavState('/' + childrenList.path)"
          >
            <template slot="title">
              <i
                :class="childrenIconList[childrenList.id]"
                class="el-icon-menu iconfont"
              ></i>
              <span>{{ childrenList.authName }}</span>
            </template>
          </el-menu-item>
        </el-submenu>
      </el-menu>
    </el-col>
  </el-aside>
</template>

<script>
export default {
  props: {
    asideListData: {
      typeof: Object
    }
  },
  data() {
    return {
      isCollapse: false,
      iconList: {
        125: 'icon-yonghuguanli',
        103: 'icon-quanxianguanli',
        101: 'icon-shangpinguanli',
        102: 'icon-dingdanguanli',
        145: 'icon-shujutongji'
      },
      childrenIconList: {
        110: 'icon-yonghuliebiao',
        111: 'icon-icon-test',
        112: 'icon-quanxianliebiao',
        104: 'icon-shangpinliebiao',
        115: 'icon-kaochafenleicanshupeizhi',
        121: 'icon-shangpinfenlei',
        107: 'icon-dingdanliebiao',
        146: 'icon-shujubaobiao'
      },
      activePath: ''
    }
  },
  created() {
    // 页面一加载就将sessionStorage里的activePath存到当前的activePath
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 点击按钮切换菜单的折叠
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态，点击之后，把path值存到sessionStorage
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
    }
  }
}
</script>
<style lang="scss" scoped>
.el-aside {
  background-color: #333744;
  transition: all 0.3s ease-out 0s;
  .toggle-button {
    background-color: #4a5064;
    font-size: 10px;
    text-align: center;
    line-height: 24px;
    color: #fff;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
  .el-menu {
    border-right: none;
  }
}
</style>
